<?php

/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/6/2
 * Time: 11:10
 * 预警管理
 */
class Warning extends MY_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('warning_model');

    }

    //预警页面
    public function index()
    {
//        echo 123123;
        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if (!$page) {
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        } else {
            $start_position = ($page - 1) * $page_num;
        }

        $data = $this->warning_model->page('warning', 0, $page_num, $start_position); //limit查询的数据


        if($data[0]){
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条
            ];
        }else{
            $viewData = [
                'list' => '',  //这里得到从数据库中的当页数据
                'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条
            ];
        }

        $this->load->view('/admin/warning/list', $viewData);


    }

    //判断post还是get参数
    public function edit()
    {
        //状态2表示已处理
        $data = [
            'status' => 2,
        ];
        $postData = $this->input->post();

        if ($postData) {
            if (!$postData) {
                redirect('/admin/warning/index?page=1');
            }

            $post = $this->input->post('del_id');
            foreach ($post as $v) {
                $where = [
                    'id' => $v
                ];

                $this->warning_model->edit($where, $data);
            }

            redirect('/admin/warning/index?page=1');
        } else {
            $where = [
                'id' => $this->input->get('id')
            ];

            //是否会有添加失败的情况?
            $resultData = $this->warning_model->edit($data, $where);

            if ($resultData) {
                redirect('/admin/warning/index?page=1');
            }
        }


    }


}